public with sharing class ConferenceItemsController {
    
    public List<ConferenceBean> qpbList { get; set; }
    public List<ConferenceItemBean>  pbList {get;set;}
    
    public ConferenceItems__c q{get;set;}
    
    public String cId { get; private set; } //市场活动id
    
    public String ItemCategory { get; set; }//检索 类别
    public String ItemName { get; set; } //检索 名称
    
    public ConferenceItemsController(ApexPages.StandardController controller) {
        cId = System.currentPageReference().getParameters().get('cid');
        qpbList= new List<ConferenceBean>();
        pbList= new List<ConferenceItemBean>();
    }
    
    public void init() {
        q=new ConferenceItems__c();
        q.MarketingActivities__c=cId;
        //如果该市场活动已经存在物品，直接显示出来
        if (String.isBlank(cId) == false){
            Schema.DescribeSobjectResult m = ConferenceItems__c.sObjectType.getDescribe();
            Map<String, Schema.SObjectField> fieldMap = m.fields.getMap();
            
            String sql = 'select ';
            String fields = '';
            for (String field : fieldMap.keySet()) {
                if (fields.length() > 0) {
                    fields += ', '; 
                }
                fields += field;
            }
            sql += fields;
            sql += ' from ConferenceItems__c where MarketingActivities__c= \'' + cId+ '\'';
            for (ConferenceItems__c qp : Database.query(sql)) {
                ConferenceBean cb=new ConferenceBean ();
                cb.conference=qp .ConferenceItems__c;
                cb.name=qp.Name;
                cb.num=qp.Number__c;
                cb.remark=qp.Remarks__c;  
                cb.chk =false;
                qpbList.add(cb);
            }
        }
        this.getGoodsList();
    }
    
    //获取下栏供选择的所有物品
   private void getGoodsList(){
       Schema.DescribeSobjectResult m = ConferenceItems__c.sObjectType.getDescribe();
       Map<String, Schema.SObjectField> fieldMap = m.fields.getMap();
            
       Schema.DescribeFieldResult sObjectFieldDescribe = fieldMap.get('ConferenceItems__c').getDescribe();
       //system.debug(sObjectFieldDescribe );
       List<Schema.PicklistEntry> lPickEntryList = sObjectFieldDescribe.getPicklistValues();
       for (Schema.PicklistEntry tPicklistEntry : lPickEntryList) {
           //因为物品名称是从下来选框值中提取出来的字符串 故把分类和名称拆分开来
           String [] arr = tPicklistEntry.getValue().split('_',2);
           ConferenceItemBean cb=new ConferenceItemBean();
           cb.conference= tPicklistEntry.getValue();
           cb.category=arr[0];
           cb.name=arr[1];
           cb.chk=false;
           pbList.add(cb);
        }
    }
    
    //保存
    public void save() {
        Savepoint sp = Database.setSavepoint();
        try {
            //删除该市场活动所有的物品
            List<ConferenceItems__c> delList = [select Id from ConferenceItems__c where MarketingActivities__c= :cId];
            if (delList.size() > 0) delete delList;
            
            // 插入已选择的物品
            if (qpbList.size() > 0) {
                List<ConferenceItems__c> insList = new List<ConferenceItems__c>();
                for (ConferenceBean qpb : qpbList) {
                    insList.add(
                        new ConferenceItems__c(
                            MarketingActivities__c  =q.MarketingActivities__c,
                            Name =qpb.conference,//直接使用物品名称
                            
                            ConferenceItems__c  = qpb.conference,
                            Number__c= qpb.num,
                            Remarks__c  = qpb.remark
                        )
                    );
                }
                insert insList;
            }
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, System.Label.Save_OK));
        } catch (Exception e) {
            Database.rollback(sp);
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, e.getMessage()));
        }
    }
    
    //检索
    public void search() {
        //因为数据量不是很大，故没有从数据库检索
        Integer hasVal=0;
        if (String.isBlank(ItemCategory ) == false) {
                hasVal+=1;
        }
        if (String.isBlank(ItemName) == false) {
                hasVal+=2;
        }
        pbList = new List<ConferenceItemBean>();
        //初始化列表
        this.getGoodsList();
        if(hasVal>0){
          List<ConferenceItemBean> pbList_temp = new List<ConferenceItemBean>();
          //检索初始化的列表
          for (ConferenceItemBean p: pbList) {
             if(hasVal==1){
               if(p.category.contains(ItemCategory ) == true){
                  pbList_temp.add(p);
                }
             }else if(hasVal==2){
                if(p.name.contains(ItemName) == true){
                   pbList_temp.add(p);
                }
             }else{
               if(p.category.contains(ItemCategory ) == true&&p.name.contains(ItemName) == true){
                   pbList_temp.add(p);
                }
             }
          }
          pbList.clear();
          pbList.addAll(pbList_temp );
          //pbList = new List<ConferenceItemBean>();
          //pbList=pbList_temp;
        }
    }
    
   
    
    //交换上下栏选中的项目
    public void exchange() {
        // 去掉上栏已勾选的的项目
        List<ConferenceBean> stayList = new List<ConferenceBean>();
        Set<String> conferenceName= new Set<String>();//为了用来判断上下栏数据是否重复
        for (ConferenceBean qpb : qpbList) {
            if (qpb.chk == false) {
                stayList.add(qpb);
                conferenceName.add(qpb.conference);
            }
        }
        qpbList.clear();
        qpbList.addAll(stayList);

        // 下栏已勾选项目 加到上面
        for (ConferenceItemBean p : pbList) {
            if (p.chk == true && conferenceName.contains(p.conference) == false) {
                ConferenceBean cb=new ConferenceBean ();
                cb.conference=p.conference;
                qpbList.add(cb);
            }
            p.chk = false;
        }
  
    }
    
    
  public class ConferenceItemBean {
        public String conference{ get; set; }
        public String category { get; set; }
        public String name { get; set; }
        public Boolean chk { get; set; }
  }
  public class ConferenceBean {
        public String activity{ get; set; }
        public String name{ get; set; }
        public String conference{ get; set; }
        public Decimal num{ get; set; }
        public String remark{ get; set; }
        public Boolean chk { get; set; }
  }

}